package com.yunxin.core.application.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunxin.common.exception.BusinessException;
import com.yunxin.core.application.command.user.AddUserTeamCommand;
import com.yunxin.core.application.service.UserTeamShipService;
import com.yunxin.core.domain.model.team.UserTeamShip;
import com.yunxin.core.infra.persistence.mapper.user.UserTeamShipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class UserTeamShipServiceImpl extends ServiceImpl<UserTeamShipMapper, UserTeamShip> implements UserTeamShipService {

    @Autowired
    private UserTeamShipMapper userTeamShipMapper;

    @Override
    public void saveUserTeamShip(Long userMerchantId, List<AddUserTeamCommand> userTeamCommands) {
        try {
            userTeamCommands.forEach(userTeamCommand -> {
                UserTeamShip userTeamShip = new UserTeamShip();
                userTeamShip.setUserMerchantId(userMerchantId);
                userTeamShip.setTeamId(userTeamCommand.getTeamId());
                userTeamShip.setReceivingStatus(userTeamCommand.getReceivingStatus());
                baseMapper.insert(userTeamShip);
            });
        } catch (Exception e) {
            throw new BusinessException("保存商户用户团队失败");
        }
    }

    @Override
    public void deleteUserTeamByUserId(Long userMerchantId) {
        try {
            baseMapper.delete(new QueryWrapper<UserTeamShip>().eq("user_merchant_id", userMerchantId));
        } catch (Exception e) {
            throw new BusinessException("修改商户用户团队失败");
        }
    }

    @Override
    public List<UserTeamShip> selectUserTeamShipByUserId(Long id) {
        try {
            return baseMapper.selectList(new QueryWrapper<UserTeamShip>().eq("user_merchant_id", id));
        } catch (Exception e) {
            throw new BusinessException("查询商户用户团队失败");
        }

    }

    @Override
    public List<UserTeamShip> selectUserTeamShipByTeamId(Long teamId) {
        try {
            return baseMapper.selectList(new QueryWrapper<UserTeamShip>().eq("team_id", teamId));
        } catch (Exception e) {
            throw new BusinessException("查询商户用户团队失败");
        }
    }

    @Override
    public List<UserTeamShip> selectUserTeamShipByTeamIds(List<Long> teamIds) {
        try {
            //this.list(new QueryWrapper<UserTeamShip>().in("team_id", teamIds));
            return userTeamShipMapper.selectUserTeamShipByTeamIds(teamIds);
        } catch (Exception e) {
            throw new BusinessException("查询商户用户团队失败");
        }
    }

    @Override
    public void deleteUserTeamByTeamId(Long teamId) {
        try {
            baseMapper.delete(new QueryWrapper<UserTeamShip>().eq("team_id", teamId));
        } catch (Exception e) {
            throw new BusinessException("删除商户用户团队关系失败");
        }
    }

}
